<?php
/**
 * Created by PhpStorm.
 * User: 35083
 * Date: 2019/7/16
 * Time: 16:36
 */

namespace app\task\controller;


class Addusermoneytask
{
    /** 任务执行函数
     * @param $key 任务唯一标识
     * @param $param 参数
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @return  返回整数，0：成功   ， 大于0失败
     */
    public function task($key, $param)
    {
        if (empty($param)) {
            return 1;
        }
        $id = $param;
        $model = new \app\admin\model\ClassUserjoin();
        $info = $model->getDataForWhere(array('id' => $id));
        if (empty($info)) {
            return 1;
        }
        if ($info['is_yuer']==1) {
            return 2;
        }
        $result = addUserMoney($info['uid'], $info['win_money'] + $info['reward_money'], 0, "挑战奖励", "");
        if ($result) {
            (new \app\admin\model\ClassUserjoin())->updateData(['is_yuer' => 1], $info['id']);
            return 0;
        } else {
            return 1;
        }
    }

    /**
     * 医院订单结算 给医生增加收入
     * @param $key
     * @param $param
     * @return int
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function htask($key, $param)
    {
        if (empty($param)) {
            return 1;
        }
        $id = $param;
        $model = new \app\admin\model\HospOrder();
        $info = $model->getDataForWhere(array('id' => $id));
        if (empty($info)) {
            return 1;
        }
        if ($info['is_yuer']==1) {
            return 2;
        }
        $result = addUserMoney($info['doctor_id'], $info['js_money'] , 0, "询诊收入", $info['order_sn']);
        if ($result) {
            $model->updateData(['is_yuer' => 1,'yuer_time'=>time()], $info['id']);
            addOrderTrackLog(array(
                'ctime'=>date("Y-m-d H:i:s"),
                'order_sn'=>$info['order_sn'],
                'op_uid'=>0,
                'op_username'=>"",
                'act'=>"结算订单并打入医生账户余额",
            ));
            return 0;
        } else {
            return 1;
        }
    }

}